#pragma once

#include <QSplitter>
#include <QList>

class DockingTabStack;
class DockingArea;

/**
 * DockingSplitter - 增强的分割器
 * 直接继承QSplitter，添加停靠系统集成和动态子节点管理
 */
class DockingSplitter : public QSplitter {
    Q_OBJECT
    
public:
    explicit DockingSplitter(Qt::Orientation orientation = Qt::Horizontal, QWidget* parent = nullptr);
    explicit DockingSplitter(DockingArea* parentArea, Qt::Orientation orientation = Qt::Horizontal, QWidget* parent = nullptr);
    ~DockingSplitter();
    
    // === 子节点管理 ===
    DockingTabStack* addTabStack(int index = -1);
    DockingSplitter* addSplitter(Qt::Orientation orientation, int index = -1);
    void removeChild(QWidget* child);
    void removeChild(int index);
    
    // === 停靠系统集成 ===
    DockingArea* parentDockingArea() const { return m_parentArea; }
    void setParentDockingArea(DockingArea* area) { m_parentArea = area; }
    
    // === 动态布局 ===
    bool isEmpty() const { return count() == 0; }
    bool hasOnlyOneChild() const { return count() == 1; }
    void collapseIfNeeded(); // 如果只有一个子节点，考虑折叠
    
    // === 子节点查询 ===
    QList<DockingTabStack*> getAllTabStacks() const;
    QList<DockingSplitter*> getAllSplitters() const;
    DockingTabStack* findTabStackContaining(QWidget* content) const;
    
    // === 分割比例管理 ===
    void setSplitRatios(const QList<double>& ratios);
    QList<double> getSplitRatios() const;
    void resetToEqualSizes();

signals:
    void childAdded(QWidget* child, int index);
    void childRemoved(QWidget* child, int index);
    void splitterBecameEmpty();
    void splitterCollapsed();

protected:
    // === Qt事件重写 ===
    void childEvent(QChildEvent* event) override;
    void resizeEvent(QResizeEvent* event) override;
    
    // === 拖放支持 ===
    void dragEnterEvent(QDragEnterEvent* event) override;
    void dragMoveEvent(QDragMoveEvent* event) override;
    void dragLeaveEvent(QDragLeaveEvent* event) override;
    void dropEvent(QDropEvent* event) override;

private slots:
    void onChildTabStackBecameEmpty();
    void onChildSplitterBecameEmpty();

private:
    // === 初始化 ===
    void setupSplitter();
    void setupConnections();
    
    // === 子节点连接管理 ===
    void connectChildSignals(QWidget* child);
    void disconnectChildSignals(QWidget* child);
    
    // === 工具函数 ===
    void cleanupEmptyChildren();
    bool shouldAutoCollapse() const;

private:
    DockingArea* m_parentArea;
    bool m_autoCollapseEnabled;
    bool m_autoRemoveEmptyChildren;
};

